<?php
class ChannelAddAction extends Action{
	
	public function main(){
		$this->_request['action'] = isset($this->_request['action']) ? $this->_request['action'] : 'default';
		switch ($this->_request['action']) {
			case 'modify':
				$this->__modify();
				break;
			default:
				$this->__load();
				break;
		}

	}

	private function __load(){
		$row = array();
		$ClassType = array(0);
		if (isset($this->_request['ChannelID'])) {
			$ChannelID = $this->_request['ChannelID'];
			$obj = new  ChannelInfoDAO();
			$row =$obj->getOneByID($ChannelID);	
			$obj2 = new ChannelClassRelation();
			$relation = $obj2->getOneByID($ChannelID);
			foreach ($row as $key => $value) {
				$this->_response[$key] = $value;
			}
			if ($relation){
				$ClassType =array ($relation['ClassID'] );
			}
			//debug($this->_response);
		}
		$selected = new Selected();
		$this->_response['ChannelEnable'] = $selected->get("ChannelEnable",$this->_response['ChannelEnable']);
		$this->_response['ChannelDisplay'] = $selected->get("ChannelDisplay",$this->_response['ChannelDisplay']);
		$this->_response['ChannelDisplayOrder'] = $selected->get("ChannelDisplayOrder",$this->_response['ChannelDisplayOrder']);
		
		$classobj = new ClassificationDAO();
		$classlist = $classobj->getList();
		$this->_response['ClassType'] = $selected->getCustomize($classlist,"ClassID","ClassName",$ClassType,1);


		//debug($ClassType);
		//debug ($this->_response);
		//$this->_response['default-row'] = $row;
	}


	private function __modify(){
		$inputarr =  array();
		$inputarr['ChannelName'] = $this->_request['ChannelName'];
		$inputarr['ChannelEnable'] = $this->_request['ChannelEnable'];
		$inputarr['ChannelDisplayOrder'] = $this->_request['ChannelDisplayOrder'];
		$inputarr['ChannelDisplay'] = $this->_request['ChannelDisplay'];
		//$inputarr['ClassType'] = $this->_request['ClassType'];
		$inputarr['Comment'] = $this->_request['Comment'];
		$inputarr['RecordFilePath'] = $this->_request['RecordFilePath'];
		$ChannelID = (int)$this->_request['ChannelID']?(int)$this->_request['ChannelID'] : 0;
		$fileupload = new Fileupload(FILE_UPLOAD_PATH.SLASH.'channel');
		//debug($_FILES["ClassIcon1"]);
		if ($_FILES["ChannelIcon1"]['tmp_name']) {
			$fileupload->run("ChannelIcon1",1);
			$inputarr['ChannelIcon1'] = FILE_UPLOAD_PATH.SLASH.'channel'.SLASH.$fileupload->saveName;
		}
		if ($_FILES["ChannelIcon2"]['tmp_name']) {
			$fileupload->run("ChannelIcon2",1);
			$inputarr['ChannelIcon2'] = FILE_UPLOAD_PATH.SLASH.'channel'.SLASH.$fileupload->saveName;
		}

		$obj = new  ChannelInfoDAO();
		$obj2 = new ChannelClassRelation();
		if ($ChannelID){
			$ret = $obj->modify($ChannelID,$inputarr);
			$newarr ['ChannelID'] = $ChannelID;
			$newarr ['ClassID'] = $this->_request['ClassType'];
			if($obj2->getOneByID($ChannelID)){
				$obj2->modify($ChannelID,$newarr);
			}else{
				$obj2->add($newarr);
			}

		}else {
			$ret = $obj->add($inputarr);
			$insertid = $obj->lastID();
			if ($insertid && $this->_request['ClassType']) {
				$newarr ['ChannelID'] = $insertid;
				$newarr ['ClassID'] = $this->_request['ClassType'];
				$obj2->add($newarr);
			}

		}


		//debug($insertid);

		//die();
		//debug($ret);
		header("Location:index.php?do=Channel/Channel_list");

		die();
		//debug($this->_request);
	}




}